<template>
  <div id="VIP_index" class="all">
    <el-breadcrumb separator="/">
      <el-breadcrumb-item>首页</el-breadcrumb-item>
      <el-breadcrumb-item>用户管理 </el-breadcrumb-item>
      <el-breadcrumb-item>会员管理</el-breadcrumb-item>
    </el-breadcrumb>

    <br>


    <div class="table-container">
      <!-- <el-descriptions title="" class="custom-descriptions"></el-descriptions> -->
      <span style="margin-right: 85%;padding: 10px;"><b>会员查询 / 添加会员</b></span>
      <el-divider class="custom-divider"></el-divider>
      <el-form :model="selform" ref="form">
        <el-row>
          <el-col :span="6">
            <el-input v-model="selform.cusId" style="width: 95%;padding: 10px;" placeholder="会员编号" />
          </el-col>
          <el-col :span="6">
            <el-input v-model="selform.cusAccount" style="width: 95%;padding: 10px;" placeholder="姓名" />
          </el-col>
          <el-col :span="6">
            <el-input v-model="selform.phone" style="width: 95%;padding: 10px;" placeholder="联系方式" />
          </el-col>
          <el-col :span="6" style="text-align: center;padding: 8px 20px;">
            <el-button type="primary" style="font-size: 16px;" @click="submitselMessage" icon="el-icon-search"> 搜索</el-button>
            <el-button type="success" style="font-size: 16px;" @click="openInputMessage"> 添加会员 </el-button>


            <el-dialog title="新增会员信息" :visible.sync="inputMessage">
              <el-form :model="inputform">
                <el-form-item label="用户名" :label-width="formLabelWidth">
                  <el-input v-model="inputform.cusAccount" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="姓名" :label-width="formLabelWidth">
                  <el-input v-model="inputform.name" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="联系方式" :label-width="formLabelWidth">
                  <el-input v-model="inputform.phone" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="初始积分" :label-width="formLabelWidth">
                  <el-input v-model="inputform.vipPoints" autocomplete="off"></el-input>
                </el-form-item>
              </el-form>
              <div slot="footer" class="dialog-footer">
                <el-button @click="closeInputMessage">取 消</el-button>
                <el-button type="primary" @click="submitInputMessage">确 定</el-button>
              </div>
            </el-dialog>

            <el-dialog title="更新会员信息" :visible.sync="updateMessage" @close="closeUpdateMessage"
              :close-on-click-modal="false">
              <el-form :model="updateform" ref="form">
                <el-form-item label="用户名" :label-width="formLabelWidth" prop="cusAccount">
                  <el-input v-model="updateform.cusAccount" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="姓名" :label-width="formLabelWidth">
                  <el-input v-model="updateform.name" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="联系方式" :label-width="formLabelWidth">
                  <el-input v-model="updateform.phone" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="积分" :label-width="formLabelWidth">
                  <el-input v-model="updateform.vipPoints" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="是否是会员" :label-width="formLabelWidth">
                  <el-select v-model="updateform.isvip" style="width: 100%" placeholder="请选择">
                    <el-option v-for="item in addoptions" :key="item.value" :label="item.label"
                      :value="item.value"></el-option>
                  </el-select>
                </el-form-item>
              </el-form>
              <div slot="footer" class="dialog-footer">
                <el-button @click="closeUpdateMessage">取 消</el-button>
                <el-button type="primary" @click="submitUpdateMessage">确 定</el-button>
              </div>
            </el-dialog>


            <el-dialog title="会员信息详情" :visible.sync="showMessage" @close="closeShowMessage" :close-on-click-modal="false">
              <el-form :model="showform" ref="form">
                <el-form-item label="用户名" :label-width="formLabelWidth">
                  <el-input v-model="showform.cusAccount" autocomplete="off" :readonly="true"></el-input>
                </el-form-item>
                <el-form-item label="姓名" :label-width="formLabelWidth">
                  <el-input v-model="showform.name" autocomplete="off" :readonly="true"></el-input>
                </el-form-item>
                <el-form-item label="联系方式" :label-width="formLabelWidth">
                  <el-input v-model="showform.phone" autocomplete="off" :readonly="true"></el-input>
                </el-form-item>
                <el-form-item label="积分" :label-width="formLabelWidth">
                  <el-input v-model="showform.vipPoints" autocomplete="off" :readonly="true"></el-input>
                </el-form-item>

              </el-form>
              <div slot="footer" class="dialog-footer">
                <el-button @click="closeShowMessage">返 回</el-button>
              </div>
            </el-dialog>

          </el-col>
        </el-row>


      </el-form>

    </div>

    <br>



    <!--表格数据-->
    <div class="table-container">
      <span style="margin-right: 91%;padding: 10px;"><b>会员信息</b></span>
      <el-divider class="custom-divider"></el-divider>
      <el-table :data="maindata" style="width: 100%" size="medium">
        <el-table-column prop="cusId" width="120" label="用户编号"></el-table-column>
        <el-table-column width="150" prop="cusAccount" label="用户名"></el-table-column>
        <el-table-column width="120" prop="name" label="姓名"></el-table-column>
        <el-table-column width="200" prop="phone" label="联系方式"></el-table-column>
        <el-table-column width="120" prop="vipPoints" label="积分"></el-table-column>
        <el-table-column label="操作" width="320" fixed="right">
          <template slot-scope="scope">
            <!-- <el-button style="font-size: 16px" type="primary" @click="openShowMessage(scope.row)"> 详情</el-button> -->
            <el-button style="font-size: 16px" type="success" @click="openUpdateMessage(scope.row)"> 修改</el-button>
            <el-button style="font-size: 16px" type="danger" @click="deleteOpen(scope.row)"> 删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <el-pagination @current-change="handleCurrentChange" :current-page="currentPage4" background
        layout="total, prev, pager, next, jumper" :total="3" :page-size="5">
      </el-pagination>

    </div>
  </div>
</template>

<script>
import axios from 'axios';
export default {
  data() {
    return {
      empId:"",
      cusId:"",
      username: "",
      password: "",
      maindata: [
      ],

      inputMessage: false,
      showMessage: false,
      updateMessage: false,


      selform: {
        cusAccount: '',
        name: '',
        phone: '',
      },
      showform: {
        cusId: '',
        cusAccount: '',
        name: '',
        phone: '',
        vipPoints: '',
      },
      inputform: {
        cusAccount: '',
        name: '',
        phone: '',
        vipPoints: '',
      },
      updateform: {
        cusId: '',
        cusAccount: '',
        name: '',
        isvip: '',
        phone: '',
        vipPoints: '',
      },
      deleteform: {
        cusId: '',
        cusAccount: '',
        name: '',
        phone: '',
        vipPoints: '',
      },

      addoptions: [
        {
          value: "0",
          label: "否",
        },
        {
          value: "1",
          label: "是",
        },
      ],

      formLabelWidth: '90px'
    };
  },
  methods: {
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
    },
    init(){
      this.empId = localStorage.getItem('empId');
      this.cusId = localStorage.getItem('cusId');
      this.username = localStorage.getItem('username')|| "";
      this.password = localStorage.getItem('password')|| "";
    },

    getMaindata() {
      const url = "http://localhost:8080/member";
      axios.get(url)
        .then((response) => {
          console.log(response.data);
          this.maindata = response.data.data.rows;
        })
        .catch((error) => {
          console.error('Error in getMaindata request', error);
        });
    },

    selDate() {
      const url = "http://localhost:8080/member";
      axios.get(url, {
        params: this.selform
      }).then((response) => {
        console.log(response.data);
        this.maindata = response.data.data.rows;
      })
        .catch((error) => {
          console.error('Error in selData request', error);
        });
    },

    delData() {
      let url = "http://localhost:8080/member";
      url += this.deleteform.cusId;
      axios.delete(url).then((response) => {
        console.log(response.data);
        this.selDate();
      })
        .catch((error) => {
          console.error('Error in delData request', error);
        });
    },

    inputData() {
      const url = "http://localhost:8080/member";
      axios.post(url, this.inputform
      ).then((response) => {
        console.log(response.data);
        this.selDate();
      })
        .catch((error) => {
          console.error('Error in inputData request', error);
        });
    },

    updateData() {
      const url = "http://localhost:8080/member";
      axios.put(url, this.updateform
      ).then((response) => {
        console.log(response.data);
        this.selDate();
      })
        .catch((error) => {
          console.error('Error in updateData request', error);
        });
    },

    openInputMessage() {
      this.inputMessage = true;
    },
    closeInputMessage() {
      this.inputMessage = false;
    },
    submitInputMessage() {
      // 处理表单提交逻辑
      this.inputData();
      // 关闭对话框
      this.$message({
        type: "success",
        message: "添加成功!",
      });
      this.submitselMessage();
      this.closeInputMessage();
    },

    openUpdateMessage(row) {
      this.updateform.cusId = row.cusId;
      this.updateform.cusAccount = row.cusAccount;
      this.updateform.name = row.name;
      this.updateform.isvip = row.isvip;
      this.updateform.phone = row.phone;
      this.updateform.vipPoints = row.vipPoints;
      this.updateMessage = true;
    },
    closeUpdateMessage() {
      this.updateMessage = false;
    },
    submitUpdateMessage() {
      this.updateData();
      this.submitselMessage();
      this.closeUpdateMessage();
      this.$message({
        type: "success",
        message: "修改成功!",
      });
    },

    openShowMessage(row) {
      this.showform.cusId = row.cusId;
      this.showform.cusAccount = row.cusAccount;
      this.showform.name = row.name;
      this.showform.phone = row.phone;
      this.showform.vipPoints = row.vipPoints;
      this.showMessage = true;
    },
    closeShowMessage() {
      this.showMessage = false;
    },

    submitselMessage() {
      this.selDate();
    },


    deleteOpen(row) {
      this.deleteform.cusId = row.cusId;
      this.deleteform.cusAccount = row.cusAccount;
      this.deleteform.name = row.name;
      this.deleteform.phone = row.phone;
      this.deleteform.vipPoints = row.vipPoints;
      this.$confirm("此操作将删除该条会员信息, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
        center: true,
      })
        .then(() => {
          this.delData();
          this.$message({
            type: "success",
            message: "删除成功!",
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
          this.submitselMessage();
        });
    },
    checkid() {
        if (this.empId == "" || this.empId == null)
          this.$router.push('/login');
      },

  },
  mounted() {
    this.init();
    this.checkid();
    this.getMaindata();

  },
};
</script>

<style>
.router-link-active {
  text-decoration: none;
}

.table-container {
  box-shadow: 0 0 4px 1px rgba(0, 0, 0, .12), 0 0 6px rgba(0, 0, 0, .12);
}

.custom-divider {
  margin: 4px 0;
  /* 调整 el-divider 的间距 */
}

.custom-descriptions {
  padding-left: 10px;
  margin-bottom: 0;
}

.all {
  line-height: 30px;
}
</style>